using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using WebControls;
using BusinessLayer;
using EntityLayer;

public partial class Admin_ArticleCategories : SFPage
{
    busArticleCategories insBusArticleCategories = new busArticleCategories();
    entArticleCategories insEntArticleCategories = new entArticleCategories();
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected bool sfgArticleCategories_NeedsDataSource()
    {
        DataTable insDataTableArticleCategories = insBusArticleCategories.SelectArticleCategories();
        if (insBusArticleCategories.HasErrors)
        {
            MessageBoxShow(insBusArticleCategories);
        }
        sfgArticleCategories.SetData(insDataTableArticleCategories);
        return insBusArticleCategories.HasErrors;
    }
    protected void btnNew_Click(object sender, EventArgs e)
    {
        this.Id = 0;
        this.OpenEditPanel();

    }
    protected void sfgArticleCategories_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "EditData")
        {
            this.Id = Convert.ToInt32(e.CommandArgument);
            this.OpenEditPanel();
        }
        else if (e.CommandName == "DeleteData")
        {
            insEntArticleCategories.ArticleCategoryId = Convert.ToInt32(e.CommandArgument);
            insBusArticleCategories.DeleteArticleCategoriesByArticleCategoryId(insEntArticleCategories);
            if (insBusArticleCategories.HasErrors)
            {
                MessageBoxShow(insBusArticleCategories);
                return;
            }
            sfgArticleCategories.LoadGridData();
            this.CloseEditPanel();
        }
    }
    public override void OpenEditPanel()
    {
        this.Clear(this.Master.FindControl("ContentPlaceHolderEdit"));
        base.OpenEditPanel();
        lstArticleCategories.LoadDataListData();

    }
    protected bool lstArticleCategories_NeedsDataSource()
    {
        insEntArticleCategories.ArticleCategoryId = this.Id;
        insBusArticleCategories.SelectArticleCategoriesByArticleCategoryId(insEntArticleCategories);
        if (insBusArticleCategories.HasErrors)
        {
            MessageBoxShow(insBusArticleCategories);
            return true;
        }
        txtArticleCategoryDescription.Text = insEntArticleCategories.ArticleCategoryDescription;

        entArticleCategoriesContents insEntArticleCategoriesContents = new entArticleCategoriesContents();
        insEntArticleCategoriesContents.ArticleCategoryId = insEntArticleCategories.ArticleCategoryId;
        busArticleCategoriesContents insBusArticleCategoriesContents = new busArticleCategoriesContents();
        DataTable insDataTableArticleCategoriesContents = insBusArticleCategoriesContents.SelectArticleCategoriesContentsByArticleCategoryId(insEntArticleCategoriesContents);
        if (insBusArticleCategoriesContents.HasErrors)
        {
            MessageBoxShow(insBusArticleCategoriesContents);
            return true;
        }
        lstArticleCategories.SetData(insDataTableArticleCategoriesContents);
        return false;
    }

    public override void Save()
    {

        this.Validate();
        if (this.HasErrors)
        {
            MessageBoxShow(this);
            return;
        }

        insEntArticleCategories.ArticleCategoryDescription = txtArticleCategoryDescription.Text;


        foreach (DataListItem dli in lstArticleCategories.Items)
        {
            entArticleCategoriesContents insEntArticleCategoriesContents = new entArticleCategoriesContents();
            if (((CheckBox)dli.FindControl("chkLanguage")).Checked)
            {
                insEntArticleCategoriesContents.LanguageId = Convert.ToInt32(((SFCheckBox)dli.FindControl("chkLanguage")).Tag);
                insEntArticleCategoriesContents.ArticleCategoryName = ((SFTextBox)dli.FindControl("txtArticleCategoryName")).Text;
                if (lstArticleCategories.DataKeys[dli.ItemIndex] != DBNull.Value)
                {
                    insEntArticleCategoriesContents.ArticleCategoriesContentId = Convert.ToInt32(lstArticleCategories.DataKeys[dli.ItemIndex]);
                    insEntArticleCategories.ArticleCategoriesContentsUpdate.Add(insEntArticleCategoriesContents);
                }
                else
                {
                    insEntArticleCategories.ArticleCategoriesContentsInsert.Add(insEntArticleCategoriesContents);
                }
            }
            else
            {
                if (lstArticleCategories.DataKeys[dli.ItemIndex] != DBNull.Value)
                {
                    insEntArticleCategoriesContents.ArticleCategoriesContentId = Convert.ToInt32(lstArticleCategories.DataKeys[dli.ItemIndex]);
                    insEntArticleCategories.ArticleCategoriesContentsDelete.Add(insEntArticleCategoriesContents);
                }
            }
        }
        base.Save();
    }
    public override void SaveNew()
    {
        insBusArticleCategories.InsertArticleCategories(insEntArticleCategories);
        if (insBusArticleCategories.HasErrors)
        {
            MessageBoxShow(insBusArticleCategories);
            return;
        }
        base.SaveNew();
        sfgArticleCategories.LoadGridData();
    }
    public override void SaveExisting()
    {
        insEntArticleCategories.ArticleCategoryId = this.Id;
        insBusArticleCategories.UpdateArticleCategoriesByArticleCategoryId(insEntArticleCategories);
        if (insBusArticleCategories.HasErrors)
        {
            MessageBoxShow(insBusArticleCategories);
            return;
        }
        base.SaveExisting();
        sfgArticleCategories.LoadGridData();
    }

    protected void lstArticleCategories_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        if (e.Item != null && e.Item.FindControl("chkLanguage") != null && e.Item.DataItem != null)
        {
            if (this.Id != 0)
            {
                ((SFCheckBox)e.Item.FindControl("chkLanguage")).Checked = (((DataRowView)e.Item.DataItem)["ArticleCategoriesContentId"] != DBNull.Value);
                e.Item.FindControl("pnlArticleCategoryContents").Visible = ((SFCheckBox)e.Item.FindControl("chkLanguage")).Checked;
            }
            else
            {
                ((SFCheckBox)e.Item.FindControl("chkLanguage")).Checked = true;
            }
            ((SFCheckBox)e.Item.FindControl("chkLanguage")).Tag = ((DataRowView)e.Item.DataItem)["LanguageId"].ToString();
        }

    }
    protected void Check(object sender, System.EventArgs e)
    {
        this.OpenEditPanelBase();
        ((SFCheckBox)sender).NamingContainer.FindControl("pnlArticleCategoryContents").Visible = ((SFCheckBox)sender).Checked;
    }
}
